package unreleased.geppetto.phraseHMM.phraseExtraction.sentencePair;


import geppetto.cat.alignments.Alignment;
import geppetto.cat.alignments.AlignmentsCache;
import geppetto.cat.alignments.AlignmentsSet;
import geppetto.phraseHMM.phraseExtraction.extractedphrase.ExtractedPhrasePair;
import geppetto.phraseHMM.phraseExtraction.phraseAccepter.KohenPhraseAccept;
import geppetto.phraseHMM.phraseExtraction.sentencePair.PhrasePairCandidate;
import geppetto.phraseHMM.phraseExtraction.sentencePair.PhrasePairCandidateExtractor;

import java.util.ArrayList;




public class AlignmentPointsPhrasePairCandidateExtractor implements PhrasePairCandidateExtractor{
	protected int size;
	protected AlignmentsSet alignments;
	
	public AlignmentPointsPhrasePairCandidateExtractor(int size, AlignmentsSet alignments) {
		super();
		this.size = size;
		this.alignments = alignments;
	}

	public PhrasePairCandidate[] extractSentencePairs(int[] foreingSentence,
			int[] sourceSentence, int sentenceNumber) {
		AlignmentsCache cache = new AlignmentsCache(alignments);

		int tSize = foreingSentence.length;
		int sSize = sourceSentence.length;

		ArrayList<Integer> sourceEndingPositions = new ArrayList<Integer>();
		ArrayList<Integer> targetEndingPositions = new ArrayList<Integer>();		
		
		for (int targetStartPos = 0; targetStartPos < tSize; targetStartPos++){
			for(int sourceStartPos = 0; sourceStartPos < sSize; sourceStartPos++){
				for(int targetEndPos = targetStartPos; targetEndPos < tSize; targetEndPos++){
					for(int sourceEndPos = sourceStartPos; sourceEndPos < sSize; sourceEndPos++){
						
						boolean hasSurePositions = false;
						for (int fpos = targetStartPos; !hasSurePositions && fpos <= targetEndPos; fpos++) {
							for (int spos = sourceStartPos; !hasSurePositions && spos <= sourceEndPos; spos++) {
								if (alignments.get(sentenceNumber).hasPosition(spos, fpos)) {
									hasSurePositions = true;
								}
							}
						}	
						
						if(hasSurePositions && !cache.hasConflits(sentenceNumber, sourceStartPos, sourceEndPos, targetStartPos, targetEndPos)){
							sourceEndingPositions.add(sourceEndPos);
							targetEndingPositions.add(targetEndPos);
							targetStartPos = targetEndPos + 1;
							sourceStartPos = sourceEndPos + 1;
							targetEndPos = targetStartPos;
							//sourceEndPos = sourceStartPos - 1; doesnt need it since its already with that value
						}
					}
				}
			}
		}
				
		ArrayList<PhrasePairCandidate> sentencePairs = new ArrayList<PhrasePairCandidate>();
		for (int targetStartPos = 0; targetStartPos < tSize; targetStartPos++){
			for(int targetEndPos = targetStartPos; targetEndPos < tSize; targetEndPos++){
				if(getPosArrayIndex(sourceEndingPositions, targetEndingPositions, targetEndPos) - getPosArrayIndex(sourceEndingPositions, targetEndingPositions, targetStartPos) < size){
					for(int sourceStartPos = 0; sourceStartPos < sSize; sourceStartPos++){
						for(int sourceEndPos = sourceStartPos; sourceEndPos < sSize; sourceEndPos++){
							sentencePairs.add(new PhrasePairCandidate(foreingSentence, sourceSentence, sourceStartPos, targetStartPos, sourceEndPos, targetEndPos));
						}
					}
				}
			}
		}		
		return sentencePairs.toArray(new PhrasePairCandidate[0]);
	}
	
	private static int getPosArrayIndex(ArrayList<Integer> sourceArray, ArrayList<Integer> targetArray, int tpos){
		int ret = 0;
		for(int i = 0; i < targetArray.size(); i++){
			Integer targetPos = targetArray.get(i);
			if(tpos < targetPos){
				break;
			}
			ret=Math.min(targetPos, sourceArray.get(i));
		}		
		return ret;
	}
	
	public static void main(String[] args){
		ArrayList<Integer> sourceArray = new ArrayList<Integer>();
		sourceArray.add(0);
		sourceArray.add(2);
		sourceArray.add(3);
		sourceArray.add(4);
		sourceArray.add(5);
		sourceArray.add(7);
		sourceArray.add(8);
		sourceArray.add(10);
		sourceArray.add(11);
		sourceArray.add(13);
		sourceArray.add(14);
		sourceArray.add(17);
		sourceArray.add(18);
		ArrayList<Integer> targetArray = new ArrayList<Integer>();
		targetArray.add(0);
		targetArray.add(1);
		targetArray.add(3);
		targetArray.add(4);
		targetArray.add(5);
		targetArray.add(7);
		targetArray.add(8);
		targetArray.add(9);
		targetArray.add(10);
		targetArray.add(11);
		targetArray.add(12);
		targetArray.add(14);
		targetArray.add(16);
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 0));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 1));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 2));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 3));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 4));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 5));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 6));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 7));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 8));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 9));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 10));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 11));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 12));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 13));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 14));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 15));
		System.out.println(getPosArrayIndex(sourceArray, targetArray, 16));
	}
}
